package com.central.apps.controller;


import com.alibaba.fastjson.JSONObject;
import com.central.app.controller.FlowCommonController;
import com.central.app.service.HandlerService;
import com.central.apps.handler.contract.ConchangeHandler;
import com.central.bizcommon.model.flow.FlowOpinion;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "合同管理接口类")
@RequestMapping("/con")
@Slf4j
public class ConClostController {

    @Autowired
    private FlowCommonController flowCommonController;
    @Autowired
    private HandlerService handlerService;
    @Autowired
    private ConchangeHandler conchangeHandler;
    @RequestMapping(value = "/startflow", method = RequestMethod.POST)
    public String clostCon(@RequestBody Map<String,Object> params) throws Exception {
        JSONObject json = new JSONObject();

        int id = MapUtils.getIntValue(params,"id");
        //查询合同验收数据，添加到合同关闭中
        //自动发起流程，
        String sql = "select * from gq_confinal where id = "+id;
        List<Map<String,Object>> list = handlerService.findBySql(sql);
        Map<String,Object> map = new HashMap<>();
        if(CollectionUtils.isNotEmpty(list)){
           map = list.get(0);
            map.put("id",null);
            //map.put("createuser", null);//创建人编号
            //map.put("createuserdesc", null);//创建人名称
            map.put("deptcode", null);//创建人名称
            map.put("constatus","待关闭");
            handlerService.insert("gq_conclose",map);
            //更改合同状态
            conchangeHandler.updateStatus(MapUtils.getString(map,"concode"), "待关闭");
        }
        //添加完毕后自动发起流程
        System.out.println("map===id==="+map.get("id"));
        List<Map<String,Object>> list2 = handlerService.findBySql("select id from gq_conclose where concode = '"+map.get("concode")+"'");
        long conid = MapUtils.getLongValue(list2.get(0),"id");
        startFlow("concloseApp",conid);
        json.put("code",0);
        json.put("msg","成功发起合同关闭");

        return json.toJSONString();
    }

    //发起流程接口
    public void startFlow(String appName,long id){
        try {
            System.out.println("id=========="+id);
            flowCommonController.startFlow(appName,id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //审批流程接口
    public void addFlow(String appname,long id){
        FlowOpinion op = new FlowOpinion();
        op.setOpinion("同意");
        Map<String,Object> objmap = new HashMap<>();
        objmap.put("direction","1");
        try {
            flowCommonController.completeFlow(appname,id,op,objmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
